﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Century.Model;
using System.Net;
using System.Web.Mvc;
using log4net;
using Century.Model.CMS;
using Century.Business.CMS;

namespace Century.Web.UI.MVC
{
    public class CenturyAdminBaseController : BaseController
    {
        public CenturyAdminBaseController(ISiteService siteService) : base(siteService) { }
      
        public ILog Log
        {
            get
            {
                return Century.EnterpriseLibrary.LogHandler.Loging.LogWithName("CMSAdminTools");
            }
        }

        protected override IActionInvoker CreateActionInvoker()
        {
            ViewBag.AccessibleSites = siteService.GetSites();// Token != null ? Token.AccessibleSites.AsEnumerable() : new List<Site>();
            return base.CreateActionInvoker();
        }

        //protected List<string> GetAvailableDomains()
        //{
        //    using (CountrySiteBSO db = new CountrySiteBSO())
        //    {
        //        List<string> addresses = new List<string>();
        //        var sites = db.GetList(c => c.IsActive);
        //        addresses = sites.Select(c => c.SiteAddress.ToLower().Replace("/" + c.Path.ToLower(), string.Empty)).Distinct().ToList();
        //        List<string> scaAddresses = sites.Select(c => c.LoginUrl.ToLower().Replace("login.aspx", string.Empty)).Distinct().ToList();
        //        List<string> demoAddresses = sites.Select(c => c.DemoApplicationUrl.ToLower().Replace(c.Path.ToLower() + "/create-demo-account", string.Empty).Replace("createdemo.aspx", string.Empty)).Distinct().ToList();
        //        List<string> liveAddresses = sites.Select(c => c.LiveApplicationUrl.ToLower().Replace(c.Path.ToLower() + "/create-live-account", string.Empty).Replace("Nationality.aspx", string.Empty)).Distinct().ToList();

        //        addresses.AddRange(scaAddresses);
        //        addresses.AddRange(demoAddresses);
        //        addresses.AddRange(liveAddresses);
        //        addresses = addresses.Distinct().ToList();
        //        return addresses;
        //    }
        //}

        protected static void CallAddress(string pageToCall)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(pageToCall);
            //httpWebRequest.Method = "POST";
            //httpWebRequest.ContentType = "text/xml; charset=utf-8";
            httpWebRequest.Timeout = 100 * 1000;
            httpWebRequest.ReadWriteTimeout = 300 * 1000;
            httpWebRequest.Timeout = 1000;

            try
            {
                HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (Exception ex)
            {
                //We know its going to fail but that dosent matter!!
            }
            finally
            {
                if (pageToCall.Contains("www."))
                {
                    CallAddress(pageToCall.Replace("www.", string.Empty));
                }
            }
        }
    }
}
